import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_count_model.dart';
import 'package:yysk_app/model/user_prop_model/list_by_propId_model.dart';
import 'package:yysk_app/model/user_prop_model/my_prop_list_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/nft_pages/danation/danation_choose_platform.dart';
import 'package:yysk_app/pages/nft_pages/my_prop/prop_history_page.dart';
import 'package:yysk_app/pages/nft_pages/my_prop/prop_batch_gift/prop_batch_gift_page.dart';
import 'package:yysk_app/pages/nft_pages/my_prop/prop_list_controller.dart';
import 'package:yysk_app/pages/nft_pages/transfer_record/transfer_out_detail_page.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/app_bar_back_widget.dart';
import 'package:yysk_app/widget/button_widget.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/utils/custom_clipper.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';

class ClipperBox extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path();
    double x = size.width;
    double y = size.height;
    double a = 35;
    path.moveTo(0, 60);
    path.quadraticBezierTo(0, a, 20, a);
    path.lineTo(x / 2, a);
    path.lineTo(x / 2 + 30, 0);
    path.lineTo(x, 0);
    path.lineTo(x, y);
    path.lineTo(0, y);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

class MyPropListPage extends GetView<PropListController> {
  static const path = '/myPropListPage';
  const MyPropListPage({super.key});
    Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Obx(()=>    Scaffold(
      // appBar: AppBar(
      //   title: Text('权益道具'),
      //   leading: AppBarBackWidget(context: context),
      //   backgroundColor: Colors.transparent,
      // ),
      body: Column(
        children: [
          Expanded(
            child: Stack(
              children: [
                SizedBox(
                  width: context.width,
                  child: ImageAssetWidget(
                    width: 375.px,
                    height: 256.px,
                    alignment: Alignment.topCenter,
                    imageUrl: 'static/images/prop/prop_bg.png',
                  ),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 21.px, 0, 0),
                  child: Column(
                    children: [
                      SizedBox(height: top),
                      CustomAppBar2(
                        title: '权益道具',
                        context: context,
                        textColors: AppColors.fontColor,
                        backgroundColor: AppColors.backgroundColor,
                      ),
                      Container(
                        padding: EdgeInsets.fromLTRB(40.px, 20.px, 0, 0),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                ImageAssetWidget(
                                  imageUrl: 'static/images/prop/prop_ico.png',
                                  width: 18.px,
                                  height: 25.px,
                                ),
                                hiSpace(width: 12.px),
                                SizedBox(
                                  width: 190.px,
                                  child: Text(
                                    '${controller.propName}',
                                    style: TextStyle(
                                      fontSize: 16.px,
                                      color: AppColors.backgroundColor,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            hiSpace(height: 20.px),
                            Row(
                              children: [
                                Text(
                                  '${controller.count}',
                                  style: TextStyle(
                                    fontSize: 30.px,
                                    fontFamily: 'CustomFont',
                                    fontWeight: FontWeight.w500,
                                    color: AppColors.backgroundColor,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Positioned(
                  left: 0,
                  top: 111.px + top + preferredSize.height,
                  right: 0,
                  bottom: 0,
                  child: ClipPath(
                    clipper: ClipperBox(),
                    child: Container(
                      color: AppColors.backgroundColor,
                      child: Stack(
                        children: [
                          Positioned(
                            right: 0,
                            bottom: 0,
                            child: ImageAssetWidget(
                              imageUrl: 'static/images/bottom_bg.png',
                              width: 258.px,
                              height: 74.px,
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.fromLTRB(14, 50, 14, 14),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(9.px),
                                    color: AppColors.backgroundColorLightGrey,
                                  ),
                                  height: 33.px,
                                  width: 116.px,
                                  child: Center(
                                    child: GestureDetector(
                                      onTap: () {
                                        Get.toNamed(
                                          '${MyPropHistoryPage.path}?propId=${controller.propId}',
                                          arguments: {'propId': controller.propId},
                                        );
                                      },
                                      child: Text(
                                        '历史道具 >',
                                        style: TextStyle(
                                          fontSize: 14.px,
                                          color: AppColors.backgroundColorBlack,
                                          fontWeight: FontWeight.normal,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                                hiSpace(height: 20.px),
                                Expanded(
                                  child: RefreshAndScrollWidget(
                                    onRefresh: _handleRefresh,
                                    scrollController: controller.scrollController,
                                    itemBuilder:
                                        (item, index) => _listItem(
                                          context: context,
                                          item: item,
                                        ),
                                    itemCount: controller.list.length,
                                    items: controller.list,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                Positioned(
                  right: 40.px,
                  top: 30.px + top + preferredSize.height,
                  child: ImageAssetWidget(
                    imageUrl: 'static/images/prop/prop_img.png',
                    width: 104.px,
                    height: 146.px,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
      // 1期隐藏
      bottomNavigationBar: controller.showBtm==true? BottomAppBar(
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 24.px, vertical: 0),
        child: ButtonWidget(
          height: 55.px,
          width: 322.px,
          fontSize: 22.px,
          label: '批量转赠',
          imageBg: 'static/images/btm.png',
          contentType: ContentType.other,
          onPressed: () {
            
               Get.toNamed('${PropBatchGiftPage.path}?propId=${controller.propId}&type=1&transferType=3',arguments: {'propId':controller.propId, 'type': '1',"transferType":'3'});
             
            // Get.toNamed(PropTransferOutPage.path);
          },
        ),
      ):SizedBox(),
    )
 );
  }

  void _handleRefresh() async {
    // 获取数据
    controller.getPropList();
    controller.getCount();
  }

  _listItem({required BuildContext context, required dynamic item}) {
    return Container(
      height: 90.px,
      padding: EdgeInsets.all(12.px),
      margin: EdgeInsets.only(bottom: 12.px),
      decoration: BoxDecoration(
        color: AppColors.backgroundColorLightGrey,
        borderRadius: BorderRadius.circular(8.px),
      ),
      child: Row(
        children: [
          LazyImage(
            imageUrl: '${item.propImageUrl}',
            height: 65.px,
            width: 65.px,
            borderRadius: 4.px,
          ),
          hiSpace(width: 16.px),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                width: 240.px,
                child: Text(
                  '${item.title}',
                  style: TextStyle(
                    fontSize: 18.px,
                    color: AppColors.backgroundColorBlack,
                    fontFamily: 'CustomFont',
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
              Text(
                item.userPropEffectiveType == 2
                    ? '有效时间：${item.invalidDate == "2099-12-31 23:59:59" ? '永久有效' : item.invalidDate}'
                    : '生效时间：${item.effectiveDate}',
                style: TextStyle(
                  fontSize: 12.px,
                  color: AppColors.fontColorGrey,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

}

// class _MyPropListPage extends State<MyPropListPage> {
//   final ScrollController scrollController = ScrollController();
//   int pageNum = 1;
//   int pageSize = 10;
//   int count = 0;//数量
//    bool showBtm = false; //是否开启转增
//   final list = RxList<ListByPropIdItem>([]);
//   late String propId;
//   late String propName;

//   @override
//   void initState() {
//     super.initState();

 
//   }
//   reflash(){
//       _getPropList();
//     _getCount();
//   }
//    @override
//   void didUpdateWidget(covariant MyPropListPage oldWidget) {
//     super.didUpdateWidget(oldWidget);
//     // 当Widget更新时，如果需要刷新，则执行刷新
//      _getPropList();
//     _getCount();

//   }
//   @override
//   void dispose() {
//     super.dispose();
//     Get.delete<ScrollController>(); //删除控制器实例
//     scrollController.dispose();
//   }

//   _getCount() {
//     http.netFetch<ListByPropIdCountModel>(
//       ApiUrl.countByPropId,
//       params: {'propId': propId},
//       success: (result) {
//         setState(() {
//           count = result.data!.count ?? 0;
//           showBtm = result.data!.supplyTransferable!;
//         });
//       },
//     );
//   }

//   _getPropList({loadMore = false}) {
//     if (loadMore) {
//       pageNum++;
//     } else {
//       pageNum = 1;
//     }

//     try {
//       http.netFetch<ListByPropIdModel>(
//         ApiUrl.listByPropId,
//         params: {'pageNum': pageNum, 'pageSize': 20, 'propId': propId},
//         success: (result) {
//           List<ListByPropIdItem> items = result.data?.list ?? [];
//           if (loadMore && items.isEmpty) {
//             pageNum--;
//           }
//           if (!loadMore) {
//             list.clear();
//           }
//           setState(() {
//             list.addAll(items);
//           });
//         },
//       );
//     } catch (e) {
//       if (loadMore) {
//         pageNum--;
//       }
//     }
//   }

// }
